package com.dragon.mmochat.model;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.model.enumType.MmochatRoleStateType;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatTeamService;

public class MmochatTeam implements Serializable {
	private static final long serialVersionUID = 5860299060448374745L;
	public static MmochatSerial serial = new MmochatSerial();
	private int teamId;
	private int teamLeaderRoleId;
	private Map<Integer, Integer> memberId = new ConcurrentHashMap<Integer, Integer>();// 所有队伍成员的roleId
	private Map<Integer, Integer> requestId = new ConcurrentHashMap<Integer, Integer>();// 请求加入队伍的列表
	private long lastClearTime = System.currentTimeMillis(); // 最近一次清理无效请求的时间,避免繁忙清理

	public MmochatTeam(MmochatPlayer teamLeader) {
		teamId = serial.addAndGetSerialId();
		MmochatTeamService.teams.put(teamId, this);

		this.teamLeaderRoleId = teamLeader.getRoleId();
		teamLeader.setTeamId(teamId);
		memberId.put(teamLeaderRoleId, teamLeaderRoleId);

		// 更新我的组队状态
		MmochatTeamService.notifyTeamStateToClient(teamLeader);

	}

	// 增加成员
	public void addMember(int roleId) {
		if (isFull()) {
			return;
		}
		MmochatPlayer he = MmochatMainService.players.get(roleId);
		if (he == null) {
			return;
		}
		MmochatPlayer leader = MmochatMainService.players.get(teamLeaderRoleId);
		if (he.isInTeam()) {
			return;
		}
		if (!he.lockState(MmochatRoleStateType.批准他人入队时)) {
			return;
		}
		if (leader.getMapId() != he.getMapId()
				|| leader.getLine() != he.getLine()) {
			he.unLockState();
			return;
		}
		if (he.isInBattle()) {
			he.unLockState();
			return;
		}
		if (he.isInTeam()) {
			he.unLockState();
			return;
		}

		// 互相加为临时好友
		for (Integer id : memberId.values()) {
			MmochatPlayer member = MmochatMainService.players.get(id);
			if (member != null) {
				member.addTempFriend(he);
				he.addTempFriend(member);
				// 下发通知
				MmochatChatService.sendSystemMsgToPerson(member, he.getName()
						+ "加入队伍!");
			}
		}
		// 下发通知
		MmochatChatService.sendSystemMsgToPerson(he, "你加入了队伍!");

		memberId.put(roleId, roleId);
		he.setTeamId(teamId);

		he.unLockState();
		// 更新组队状态
		MmochatTeamService.notifyTeamStateToClient(he);
	}

	// 踢出成员
	public boolean delMember(int roleId) {
		if (memberId.containsKey(roleId)) {
			String name;
			MmochatPlayer he = MmochatMainService.getRoleFromBuffer(roleId);
			if (he != null) {
				he.leaveTeam();

				// 更新组队状态
				MmochatTeamService.notifyTeamStateToClient(he);
				name = he.getName();
			} else {
				name = "一位队员";
			}
			memberId.remove(roleId);

			// 向其它队员下发信息
			for (Integer id : memberId.values()) {
				MmochatPlayer member = MmochatMainService.players.get(id);
				if (member != null) {
					// 下发通知
					MmochatChatService.sendSystemMsgToPerson(member, name
							+ "离开队伍!");
				}
			}
			return true;
		}
		return false;
	}

	// 解散队伍
	public void dismissTeam() {
		for (Integer roleId : memberId.values()) {
			MmochatPlayer member = MmochatMainService.players.get(roleId);
			if (member != null) {
				member.leaveTeam();
				// 更新组队状态
				MmochatTeamService.notifyTeamStateToClient(member);
				// 向此玩家下发离队信息
				MmochatChatService.sendSystemMsgToPerson(member, "队伍已解散!");
			}
		}
		memberId.clear();
		MmochatTeamService.teams.remove(teamId);
	}

	// 队伍中同帮派的人数
	public int getSameGangMemberNum(int gangId) {
		int count = 0;
		for (Integer roleId : memberId.values()) {
			MmochatPlayer member = MmochatMainService.players.get(roleId);
			if (member != null && member.hasGang()) {
				if (member.getGangId() == gangId) {
					count++;
				}
			}
		}
		return count;
	}

	public void leaveTeam(int roleId) {
		if (isTeamLeader(roleId)) {
			dismissTeam();
		} else {
			delMember(roleId);
		}
	}

	// 增加入队请求
	public int addRequest(int roleId) {
		clearDeadRequest();
		if (isFull()) {
			// 队伍已满
			return 1;
		}
		if (isRequestFull()) {
			// 申请人数已满
			return 2;
		}
		requestId.put(roleId, roleId);
		return 0;
	}

	// 删除入队请求
	public void delRequest(int roleId) {
		requestId.remove(roleId);
	}

	// 清除无效的入队请求
	public void clearDeadRequest() {
		if (System.currentTimeMillis() - lastClearTime < 5000) {
			return;
		}
		lastClearTime = System.currentTimeMillis();
		MmochatPlayer leader = MmochatMainService.players.get(teamLeaderRoleId);
		for (int roleId : requestId.values()) {
			MmochatPlayer p = MmochatMainService.players.get(roleId);
			if (p == null || p.isDeadConnection()) {
				// 离线或掉线
				requestId.remove(roleId);
			} else if (p.isInTeam()) {
				// 已经有队
				requestId.remove(roleId);
			} else if (leader != null && !leader.isInSameMapLine(p)) {
				// 和队长不在一个地图及线
				requestId.remove(roleId);
			}
		}
		lastClearTime = System.currentTimeMillis();
	}

	// 入队请求已满
	public boolean isRequestFull() {
		return requestId.size() >= 10;
	}

	// 获取队员人数
	public int getMemberNum() {
		return memberId.size();
	}

	// 是否为队长
	public boolean isTeamLeader(int roleId) {
		return roleId == teamLeaderRoleId;
	}

	public boolean isFull() {
		return memberId.size() >= 5;
	}

	public int getTeamId() {
		return teamId;
	}

	public void setTeamId(int teamId) {
		this.teamId = teamId;
	}

	public int getTeamLeaderRoleId() {
		return teamLeaderRoleId;
	}

	public void setTeamLeaderRoleId(int teamLeaderRoleId) {
		this.teamLeaderRoleId = teamLeaderRoleId;
	}

	public Map<Integer, Integer> getMemberId() {
		return memberId;
	}

	public void setMemberId(Map<Integer, Integer> memberId) {
		this.memberId = memberId;
	}

	public Map<Integer, Integer> getRequestId() {
		return requestId;
	}

	public void setRequestId(Map<Integer, Integer> requestId) {
		this.requestId = requestId;
	}

}
